package com.learn.mapper;

import com.learn.pojo.Order;
import com.learn.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author suyafei
 */
public interface IUserMapper {


    /**
     * 查询所有用户。同时查询每个用户的关联的订单
     *
     * @return
     */
    @Select("select * from user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "orderList", column = "id", javaType = List.class,
                    many = @Many(select = "com.learn.mapper.IOrderMapper.findOrderByUid")),
    })
    public List<User> findAll();

    /**
     * 查询所有用户，同时查询每个用户关联的角色信息
     *
     * @return
     */
    @Select("select * from user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "roleList", column = "id",javaType = List.class,
                many = @Many(select = "com.learn.mapper.IRoleMapper.findRoleByUId")
            ),
    })
    public List<User> findAllUserAndRole();

    /**
     * 注解开发：添加用户
     * @param user
     */
    @Insert("insert into user(id,username) values(#{id},#{username})")
    public void addUser(User user);


    @Update("update user set username = #{username} where id = #{id}")
    public void updateUser(User user);


    @Select("select * from user")
    public List<User> selectUser();


    @Delete("delete from user where id =#{id}")
    public void deleteUser(Integer id);

    @Select("select * from user where id = #{id}")
    public User findUserById(Integer id);


}
